class Impulse {
private:
  int position_x_;
  int position_y_;

  float power_;
  float maxPower_;
  float growth_;

  float radius_;

public:
  bool on;

  Impulse() {
    position_x_ = 0;
    position_y_ = 0;

    power_ = 0.0f;
    maxPower_ = 50.0f;
    growth_ = 0.33f;

    radius_ = 20;
    on = false;
  }

  void setPosition(int posX, int posY) {
    position_x_ = posX;
    position_y_ = posY;
  }

  void update(Balloon *target) {
    if (on) {
      power_ = maxPower_;
    } else {
      if (power_ > 0.0f) {
        power_ -= growth_;
      }
            
      if (power_ < 0.0f) {
        power_ = 0.0;
      }
    }

    if (target->isAttached()) {
      // Influence the balloon
      if (on) {
        float dx = position_x_ - target->getX();
        float dy = position_y_ - target->getY();

        float distance = sqrt(dx * dx + dy * dy);

        if (target->isAttached()) {

          int level = 0;
          if (distance < 50) {
            level = 100;
          } else if (distance < 75) {
            level = 10;
          } else if (distance < 100) {
            level = 1;
          }

          if (level != 0) {
          
            float angle = 0;
            if (dy == 0) {
              angle = 0;
            } else {
              angle = atan(dy / dx);
            }
            
            float infX = cos(angle) * power_ * level / 100000;
            float infY = sin(angle) * power_ * level / 100000;

            int relX = 0;
            if (dx > 0) {
              relX = -1;
            } else if (dx < 0) {
              relX = 1;
            }
            infX *= relX;

            int relY = 0;
            if (dx > 0) {
              relY = -1;
            } else if (dx < 0) {
              relY = 1;
            }
            infY *= relY;

            target->influence(infX, infY);
          }
        }
      }
    }
  }


  void draw(mat4 &modelToProjection, shader &shader) {
  }

  ~Impulse() {}
};
